package main

import (
	"encoding/json"
	"net/http"
	"strconv"
	"strings"
	"time"
)

type ImpInterface interface {
}

// ImpInterfaceImp 接口模型实现
type ImpInterfaceImp struct{}

// Imp 实现实例
var Imp ImpInterface = &ImpInterfaceImp{}

func (this ImpInterfaceImp) TestApi(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	SuccessJson(w, Info, Info)

	return
}
func UserGet_login(username string, psd string, project string, lastlogintime int64) (bool, map[string]string) {
	f, e := redisDB.HGet(ctx, project+":useradmin", username).Result()
	if e != nil {
		return false, nil
	}
	dm := map[string]string{}
	e2 := json.Unmarshal([]byte(f), &dm)
	if e2 != nil {
		return false, nil
	}
	if psd != dm["Psd"] || dm["Enable"] != "1" {
		return false, nil
	}
	if lastlogintime > 0 {
		LoginTime := Str2Int64(dm["LoginTime"])
		if LoginTime > 0 && lastlogintime+LoginTime > time.Now().Unix() {
			return true, dm
		}
		if LoginTime <= 0 {
			return true, dm
		}
		return false, nil
	}

	return true, dm
}
func (this ImpInterfaceImp) User_login(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	username := Info.UrlPath[2]
	psd := Info.UrlPath[3]
	if Info.UrlPath[4] == "null" {
		Info.UrlPath[4] = "1:2:3"
	}
	UserLevel := strings.Split(Info.UrlPath[4], ":")

	isLoginSuccess, userdata := UserGet_login(username, psd, Info.Project, 0)
	if isLoginSuccess == false {
		ErrorJson(w, 2, "用户名或密码错误", Info)
		return
	}
	no := 0
	for _, v := range UserLevel {
		if v == userdata["Level"] {
			no = 1
		}
	}
	if no == 0 {
		ErrorJson(w, 3, "权限不足", Info)
		return
	}
	ips := ""
	for k, _ := range Info.IP {
		if ips == "" {
			ips = k
		} else {
			ips = ips + "&" + k
		}
	}
	SuccessJson(w, JSAesEncrypt2(username+":"+psd+":"+strconv.FormatInt(time.Now().Unix(), 10)+":"+ips), Info)

	return
}
func (this ImpInterfaceImp) Get_login(w http.ResponseWriter, r *http.Request, Info InfoStruct) {

	SuccessJson(w, Info.UserInfo, Info)
	return
}

func (this ImpInterfaceImp) Set_Hmset(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	dd := map[string]interface{}{}
	er := json.Unmarshal([]byte(Info.Body), &dd)
	if er != nil || len(dd) == 0 {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	_, e := redisDB.HMSet(ctx, Info.Project+":"+Info.UrlPath[2], dd).Result()
	if e == nil {
		SuccessJson(w, "", Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Get_Hmget(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	dd := []string{}
	er := json.Unmarshal([]byte(Info.Body), &dd)
	if er != nil || len(dd) == 0 {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	f, e := redisDB.HMGet(ctx, Info.Project+":"+Info.UrlPath[2], dd...).Result()
	if e == nil {
		SuccessJson(w, f, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Get_Hmgets(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	dd := map[string][]string{}
	er := json.Unmarshal([]byte(Info.Body), &dd)
	if er != nil || len(dd) == 0 {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	data := map[string][]interface{}{}
	for tablename, v := range dd {
		if tablename != "useradmin" {
			data[tablename], _ = redisDB.HMGet(ctx, Info.Project+":"+tablename, v...).Result()
		}
	}
	SuccessJson(w, data, Info)
	return
}

func (this ImpInterfaceImp) Get_Hgetalls(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	dd := []string{}
	er := json.Unmarshal([]byte(Info.Body), &dd)
	if er != nil || len(dd) == 0 {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	data := map[string]map[string]string{}
	for _, tablename := range dd {
		if tablename != "useradmin" {
			data[tablename], _ = redisDB.HGetAll(ctx, Info.Project+":"+tablename).Result()
		}
	}
	SuccessJson(w, data, Info)
	return
}
func (this ImpInterfaceImp) Set_Hincrby(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" || Info.UrlPath[3] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	incr := Str2Int64(Info.UrlPath[4])
	d, e := redisDB.HIncrBy(ctx, Info.Project+":"+Info.UrlPath[2], Info.UrlPath[3], incr).Result()
	if e == nil {
		SuccessJson(w, d, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Set_Hset(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" || Info.UrlPath[3] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	_, e := redisDB.HSet(ctx, Info.Project+":"+Info.UrlPath[2], Info.UrlPath[3], Info.UrlPath[4]).Result()
	if e == nil {
		SuccessJson(w, "", Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Get_Hget(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" || Info.UrlPath[3] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	f, e := redisDB.HGet(ctx, Info.Project+":"+Info.UrlPath[2], Info.UrlPath[3]).Result()
	if e == nil {
		SuccessJson(w, f, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Get_Hgetall(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	f, e := redisDB.HGetAll(ctx, Info.Project+":"+Info.UrlPath[2]).Result()
	if e == nil {
		SuccessJson(w, f, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Set_Rpush(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" || Info.UrlPath[3] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	f, e := redisDB.RPush(ctx, Info.Project+":"+Info.UrlPath[2], Info.UrlPath[3]).Result()
	if e == nil {
		SuccessJson(w, f, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
func (this ImpInterfaceImp) Get_lrange(w http.ResponseWriter, r *http.Request, Info InfoStruct) {
	//微信号
	if Info.UrlPath[2] == "" || Info.UrlPath[3] == "" {
		ErrorJson(w, 1, "参数错误", Info)
		return
	}
	f, e := redisDB.LRange(ctx, Info.Project+":"+Info.UrlPath[2], Str2Int64(Info.UrlPath[3]), Str2Int64(Info.UrlPath[4])).Result()
	if e == nil {
		SuccessJson(w, f, Info)
		return
	}
	ErrorJson(w, 2, "错误", Info)
	return
}
